﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExaminationSys.DataAccess;
using ExaminationSys.DataAccess.Repository;
using Ext.Net;
using ExaminationSys.Helper;
using ExaminationSys.DataAccess.Entity;

namespace ExaminationSys.View.Section
{
    public partial class SectionInfoEdit : System.Web.UI.UserControl
    {
        private static readonly SectionInfoService Proxy = ObjectFactory<SectionInfoService>.Create();
        private static SectionInfo entity = new SectionInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                entity = null;
                Bind();
            }
        }
        private void Bind()
        {

            //SectionType.DataSource = CommonEnum.EnumToDictionary(typeof(EnumSectionType));
            //this.SectionTypeID.GetStore().DataBind();
        }

        public Store GridStore { get; set; }
        public string ID { get; set; }


        public void SetData(string id)
        {
            SectionInfoEditPanel.Reset();
            if (!string.IsNullOrEmpty(id))
            {
                var realId = new Guid(id);
                var sectionInfo = Proxy.Get(realId);

                if (sectionInfo != null)
                {
                    entity = sectionInfo;
                    OrderIndex.Text = sectionInfo.OrderIndex.ToString();
                    Name.Text = sectionInfo.Name;
                    if(!string.IsNullOrEmpty(sectionInfo.Img)){
                         Img.Visible = true;
                         Imgload.Visible = false;
                         reUploadImgLBtn.Visible = true;
                    }
                    Img.Text = sectionInfo.Img;
                    if (sectionInfo.SectionTypeName != "")
                    {
                        SectionType.Value = sectionInfo.SectionTypeName;
                        //SectionType.SelectedItem.Value = sectionInfo.SectionTypeID.ToString();
                    }
                    if (sectionInfo.ID != null)
                    {
                        IDHidden.Value = sectionInfo.ID.ToString();
                        X.Js.Call("setID", sectionInfo.ID);
                    }
                }
            }
            else
            {
                entity = new SectionInfo();
                Imgload.Clear();
                Imgload.Reset();
        
                 
                Name.Text = "";
                Img.Text = "";
                Img.Visible = false;
                Imgload.Visible = true;
                reUploadImgLBtn.Visible = false;
                IDHidden.Value = "";
               

            }




        }

        public void Show()
        {
            this.SectionInfoEditWindow.Show();
        }

        protected void reUploadImgLBtn_Click(object sender, DirectEventArgs e)
        {
            Img.Visible = false;
            Imgload.Visible = true;

        }

        protected void SaveSectionInfo(object sender, DirectEventArgs e)
        {
            // public int sectionTypeID = SectionTypeID.Value;
            string name = Name.Text;
            string img = Img.Text;
            int orderIndex = int.Parse(OrderIndex.Text);
         
            DateTime OpeatorTime = DateTime.Now;
            int status = 1;


            if (string.IsNullOrEmpty(name))
            {
                MessageBoxHelper.ShowWarning("name不能为空");
                return;
            }

            //Guid realId = string.IsNullOrEmpty(id) ? Guid.NewGuid() : new Guid(id);
            //if (Proxy.CheckTitleExists(title, realId))
            //{
            //    MessageBoxHelper.ShowWarning(string.Format("标题 {0} 已经存在", title));
            //    return;

            //}
            entity.Img = img;
            if (Imgload.HasFile)
            {
                   string FileName = this.Imgload.FileName.Trim();
                   string FilePath = "/UpLoad/" + FileName;
                   System.Web.HttpFileCollection _files = System.Web.HttpContext.Current.Request.Files;
                   System.Web.HttpPostedFile _postedFile = _files[0];

                   if (!string.IsNullOrEmpty(FilePath))
                   {
                       if (System.IO.File.Exists(System.Web.HttpContext.Current.Request.MapPath(FilePath)))
                       {
                           System.IO.File.Delete(System.Web.HttpContext.Current.Request.MapPath(FilePath));
                       }
                       _postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath(FilePath));
                   }

                   entity.Img = FilePath;
            }

            entity.Name = name;
            entity.OrderIndex = orderIndex;
            try
            {
                entity.SectionTypeID = int.Parse(SectionType.SelectedItem.Value);
            }
            catch
            {
                entity.SectionTypeID = CommonEnum.GetKeyBy(SectionType.SelectedItem.Value, typeof(EnumSectionType));
            }
            entity.SectionTypeName = SectionType.Value.ToString();
            entity.Status = status;

            var op = FormsAuthenticationHelper.GetCurrentUserID();
            if (string.IsNullOrEmpty(op))
            {
                FormsAuthenticationHelper.SignOut();
                Response.Redirect("~/Account/Login.aspx");
            }
            entity.Operator = op;

            entity.OperatorTime = DateTime.Now;
            var sucess = false;
            // entity.
            if (string.IsNullOrWhiteSpace(IDHidden.Value))
            {
                var retCode = Proxy.Insert(entity);
                sucess = retCode != null;
            }
            else
            {
                sucess = Proxy.Update(entity);
            }

            if (sucess)
            {
                MessageBoxHelper.ShowInfo("保存成功");
                GridStore.Reload();
                SectionInfoEditWindow.Hide();
            }
            else
            {
                MessageBoxHelper.ShowInfo("保存失败");
                return;

            }
        }

    }
}